<?php
/**
 * User
 *-------------------------
 *
 * Model user, all functions to deal with user add, change, delete 
 *
 * PHP versions 5
 *
 * LICENSE: This source file is from Smarter Ver4.0, which is a comprehensive shopping engine
 * that helps consumers to make smarter buying decisions online. We empower consumers to compare
 * the attributes of over one million products in the computer and consumer electronics categories
 * and to read user product reviews in order to make informed purchase decisions. Consumers can then
 * research the latest promotional and pricing information on products listed at a wide selection of
 * online merchants, and read user reviews on those merchants.
 * The copyrights is reserved by http://www.mezimedia.com.
 * Copyright (c) 2005, Mezimedia. All rights reserved.
 *
 * @author     kevin cheng<kevin_cheng@mezimedia.com>
 * @copyright  (C) 2004-2008 Mezimedia.com
 * @license    http://www.mezimedia.com  PHP License 5.2
 * @version    CVS: $Id: UserController.php
 * @link       http://pms.mezimedia.com/
 * @deprecated File deprecated in Release 1.0
 */

class User extends Zend_Db_Table
{	
	public $_db = null;
	public $_filter_list =array();	
	public $_totalCount;
	private $sqlFilter = "";
	
	/*
	 * set the db handle by fetching from register
	 * init the db table setting 
	 * 
	 */
	public function __construct () {
		$this->_db = Zend_Registry :: get('db_adapter');
		$this->_name = 'tt_user';
		$this->_primary = 'id';
		parent::_setup();
	}
	
	/*
	 * get user list from user, together with team, role
	 * 
	 * @param string $arg1  the string to quote
     *
     * @return array user info               
     * @throws db error or exceptions
     * @access protected    
	 */
	function getFullUserList($where=array(), $order='', $from=0, $size=20){		
		if($where['name'] !=""){
			$this->sqlFilter .= "And u.name like '%".$where['name']."%' "; 
		}
		if($where['tt_team_id'] >0){
			$this->sqlFilter .= "And u.tt_team_id =".$where['tt_team_id']." ";
		}
		if($where['tt_role_id'] >0){
			$this->sqlFilter .= "And u.tt_role_id =".$where['tt_role_id']." ";
		}
		//$this->sqlFilter = $sqlFilter;
		
		$userList = array();		
		$rs = $this->_db->query('select SQL_CALC_FOUND_ROWS u.id as userId, u.name as userName, u.email, u.phone, r.id as roleId, r.name as roleName, t.id as teamId, t.name as teamName from tt_user u, tt_role r, tt_team t where u.tt_team_id = t.id and u.tt_role_id = r.id '.$this->sqlFilter.' order by '.$order.' limit '.$from.",".$size);		
		if ($rs) {
			$userList= $rs->fetchAll();												
		}
		$totalCountRS = $this->_db->query('SELECT FOUND_ROWS() as count');
		$count = $totalCountRS->fetchAll();		
		$this->_totalCount = $count[0]["count"];
		return $userList;
	}
	
	/*get a user's basic info */
	function getUser($userId){
		$rs = $this->_db->query('select u.id as userId, u.name as userName, u.passwd, u.email, u.phone, r.id as roleId, r.name as roleName, t.id as teamId, t.name as teamName from tt_user u, tt_role r, tt_team t where u.id='.$userId.' and u.tt_team_id = t.id and u.tt_role_id = r.id');		
		if ($rs) {
			$user= $rs->fetchAll();												
		}		
		return $user[0]; 
	}
	
	/*
	 * Get all unique teams based on current users
	 */
	function getUserTeams(){
		$rs = $this->_db->query('select distinct t.id as teamId, t.name as teamName from tt_user u, tt_team t where u.tt_team_id = t.id and u.flag=1 '.$this->sqlFilter.' order by t.name');
		return $rs->fetchAll();
	}
	
	/*
	 * Get all unique roles based on current users
	 */
	function getUserRoles(){
		$rs = $this->_db->query('select distinct r.id as roleId, r.name as roleName from tt_user u, tt_role r where u.tt_role_id = r.id and u.flag=1 '.$this->sqlFilter.' order by r.name');
		return $rs->fetchAll();
	}
	
	
	function findUser($where)
	{
		$rs = $this->_db->query('select * from tt_user u where '.$where);		
		return $rs->fetchAll();
	}
	
}
?>
